package com.zh.note.huawei.onehundred.符合条件的子串长度;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: zhanghuan
 * @date: 2023/9/4 14:15
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        // String line1 = sc.nextLine();
        // String line2 = sc.nextLine();
        // int maxDiff = sc.nextInt();
        String line1 = "xxcdefg";
        String line2 = "cdefghi";
        int maxDiff = 5;
        // method1(line1, line2, maxDiff);
        method2(line1, line2, maxDiff);
    }

    private static void method2(String line1, String line2, int maxDiff) {
        int maxLength = 0;
        int length = 0;
        int count = Math.abs(line1.charAt(0) - line2.charAt(0));
        List<Integer> list = new ArrayList<>();
        list.add(count);
        for (int i = 1; i < line1.length(); i++) {
            int temp = Math.abs(line1.charAt(i) - line2.charAt(i));
            list.add(temp);
            count += temp;
            if (count <= maxDiff) {
                length = list.size();
            } else {
                count -= list.get(0);
                list.remove(0);
            }
            maxLength = Math.max(length, maxLength);
        }
    }

    private static void method1(String line1, String line2, int maxDiff) {
        int res = 0;
        for (int i = 0; i < line1.length(); i++) {
            int count = 0;
            int sum = 0;
            for (int j = i; j < line1.length(); j++) {
                sum += Math.abs(line1.charAt(j) - line2.charAt(j));
                if (sum <= maxDiff) {
                    count++;
                } else { // 不符合条件，重新统计
                    break;
                }
            }
            if (count > res) {
                res = count;
            }
        }
        System.out.println(res);
    }

}
